package com.kaka.service.serviceimpl;

import com.kaka.dao.StudentDao;
import com.kaka.dao.TeacherDao;
import com.kaka.dao.daoimpl.StudentDaoImpl;
import com.kaka.dao.daoimpl.TeacherDaoImpl;
import com.kaka.bean.Student;
import com.kaka.service.TeacherService;
import com.kaka.util.SMSUtil;

import java.util.List;
import java.util.Random;

public class TeacherServiceImpl implements TeacherService {

    private final StudentDao studentDao = new StudentDaoImpl();
    private final TeacherDao teacherDao = new TeacherDaoImpl();

    @Override
    public List<Student> showMyStudent(int tid) {
        return studentDao.selectAllByTid(tid);
    }

    @Override
    public void updateStudentScore(int sid, double score) {
        SMSUtil.check(studentDao.updateScoreBySid(sid, score));
    }

    @Override
    public String randomSomeone(int tid, int status) {
        List<Student> studentList = studentDao.selectByTidAndStatus(tid, status);
        Random random = new Random();
        Student student = studentList.get(random.nextInt(studentList.size()));
        studentDao.updateStatusBySid(student.getSid(), 1);
        if (studentList.size() == 1) {
            studentDao.updateStatusByTid(tid, 0);
        }
        return student.getName();
    }

    @Override
    public void updateTeacherPassword(int tid, String newPassword) {
        SMSUtil.check(teacherDao.updatePasswordByTid(tid, newPassword));
    }
}
